﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using UtilityLibrary;

namespace MapLibrary
{
    class TiledEditorLayer : EditorLayer
    {
        private int tileWidth;
        private int tileHeight;

        public TiledEditorLayer(Layer pLayer) : base(pLayer)
        {
            tileWidth = 100;
            tileHeight = 100;
        }

        public override void AddNode(EditorNode pNode)
        {
            int tileX = (int)Math.Floor((float)pNode.Position.X / tileWidth);
            int tileY = (int)Math.Floor((float)pNode.Position.Y / tileHeight);

            Vector2 pos = pNode.Position;
            pos.X = (tileX * tileWidth) + (tileWidth / 2);
            pos.Y = (tileY * tileHeight) + (tileHeight / 2);
            pNode.Position = pos;

            RemoveNodeAtPosition(pos);

            base.AddNode(pNode);
        }

        private System.Collections.IEnumerable TilePointsInWorldHoriz(int startx, int endx)
        {
            int start = startx + (tileWidth - (startx % tileWidth)) - tileWidth;
            int end = endx + (tileWidth - (endx % tileWidth));

            for (int i = start; i <= end; i += tileWidth)
            {
                yield return i;
            }
        }

        private System.Collections.IEnumerable TilePointsInWorldVert(int starty, int endy)
        {
            int start = starty + (tileHeight - (starty % tileHeight)) - tileHeight;
            int end = endy + (tileHeight - (endy % tileHeight));

            for (int i = start; i <= end; i += tileHeight)
            {
                yield return i;
            }
        }

        public override void Draw(SpriteBatch batch, WorldViewport viewport)
        {
            base.Draw(batch, viewport);

            int startX = (int)viewport.X;
            int endX = (int)viewport.X + (int)viewport.Width;
            int startY = (int)viewport.Y;
            int endY = (int)viewport.Y + (int)viewport.Height;

            Vector2 worldPos1 = new Vector2();
            Vector2 worldPos2 = new Vector2();
            //draw tile lines

            //vertical lines
            foreach (int x in TilePointsInWorldHoriz(startX, endX))
            {
                worldPos1.X = x;
                worldPos1.Y = viewport.Y;
                worldPos2.X = x;
                worldPos2.Y = viewport.Y + viewport.Height;

                worldPos1 = viewport.WorldToViewportCoords(worldPos1);
                worldPos2 = viewport.WorldToViewportCoords(worldPos2);

                PrimitiveBrush.RenderLine(batch, worldPos1, worldPos2, Color.Beige);
            }

            //horizontal lines
            foreach (int y in TilePointsInWorldVert(startY, endY))
            {
                worldPos1.X = viewport.X;
                worldPos1.Y = y;
                worldPos2.X = viewport.X + viewport.Width;
                worldPos2.Y = y;

                worldPos1 = viewport.WorldToViewportCoords(worldPos1);
                worldPos2 = viewport.WorldToViewportCoords(worldPos2);

                PrimitiveBrush.RenderLine(batch, worldPos1, worldPos2, Color.Beige);
            }
        }

        private void RemoveNodeAtPosition(Vector2 pos)
        {
            foreach(EditorNode tNode in this.mEditorNodes)
            {
                if (tNode.Position.Equals(pos))
                {
                    this.mEditorNodes.Remove(tNode);
                    break;
                }
            }
        }
    }
}
